package com.qdxht.dispenser.ui.activity.test;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.request.RequestOptions;
import com.shuyu.gsyvideoplayer.listener.GSYSampleCallBack;
import com.qdxht.dispenser.R;
import com.youth.banner.adapter.BannerAdapter;
import java.util.ArrayList;
import java.util.HashMap;

public class PlayAdapter extends BannerAdapter<PlayResourcesBean, RecyclerView.ViewHolder> {
    private static final int VIDEO = 1;
    private static final int IMAGE = 2;

    private final Context mContext;
    private final BannerActivity mActivity;
    private final HashMap<Integer, RecyclerView.ViewHolder> mVHMap;
    private final RequestOptions options;

    public PlayAdapter(Context context, ArrayList<PlayResourcesBean> dataList, BannerActivity activity) {
        super(dataList);
        this.mContext = context;
        this.mActivity = activity;
        this.mVHMap = new HashMap<>();
        this.options = new RequestOptions()
                .priority(Priority.HIGH)
                .placeholder(R.mipmap.ic_launcher)
                .error(R.mipmap.ic_launcher);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position, @NonNull java.util.List<Object> payloads) {
        super.onBindViewHolder(holder, position, payloads);

        int realPosition = getRealPosition(position);
        if (holder instanceof VideoHolder) {
            setVideo((VideoHolder) holder, getData(realPosition), position, realPosition, getRealCount());
            mVHMap.put(position, holder);
        } else if (holder instanceof ImageHolder) {
            setImage((ImageHolder) holder, getData(realPosition), realPosition, getRealCount());
            mVHMap.put(position, holder);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return getData(getRealPosition(position)).isImg() ? IMAGE : VIDEO;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        if (viewType == VIDEO) {
            View view = inflater.inflate(R.layout.play_item_video, parent, false);
            return new VideoHolder(view);
        } else {
            View view = inflater.inflate(R.layout.play_item_image, parent, false);
            return new ImageHolder(view);
        }
    }

    private void setImage(ImageHolder holder, PlayResourcesBean data, int position, int size) {
        Glide.with(mContext)
                .load(data.getUrl())
                .apply(options)
                .into(holder.image);

//        holder.title.setText(data.getTitle());
//        holder.numIndicator.setText(String.format("%d/%d", position + 1, size));
    }

    private void setVideo(VideoHolder holder, PlayResourcesBean data, int position, int realPosition, int size) {
        holder.video.setUp(data.getUrl(), true, mContext.getExternalCacheDir(), null, "");
        holder.video.getTitleTextView().setVisibility(View.GONE);
        holder.video.getBackButton().setVisibility(View.GONE);
        holder.video.getFullscreenButton().setVisibility(View.GONE);
        holder.video.getStartButton().setVisibility(View.GONE);

        holder.video.setReleaseWhenLossAudio(true);
        holder.video.setAutoFullWithSize(false);
        holder.video.setDismissControlTime(0);
        holder.video.setClickable(false);
        holder.video.setEnabled(false);
        holder.video.setLongClickable(false);
        holder.video.setPlayPosition(position);

        holder.video.setIsTouchWiget(false);
        holder.video.setVideoAllCallBack(new GSYSampleCallBack() {
            @Override
            public void onComplete(String url, Object... objects) {
                super.onComplete(url, objects);
                mActivity.getBanner().isAutoLoop(true);
                mActivity.getBanner().start();
            }

            @Override
            public void onPlayError(String url, Object... objects) {
                super.onPlayError(url, objects);
                mActivity.getBanner().isAutoLoop(true);
                mActivity.getBanner().start();
            }

            @Override
            public void onClickStop(String url, Object... objects) {
                super.onClickStop(url, objects);
                mActivity.getBanner().isAutoLoop(true);
                mActivity.getBanner().start();
            }

            @Override
            public void onAutoComplete(String url, Object... objects) {
                super.onAutoComplete(url, objects);
                mActivity.getBanner().isAutoLoop(true);
                mActivity.getBanner().setLoopTime(100);
                mActivity.getBanner().start();
                mActivity.getBanner().setLoopTime(mActivity.getLoopTime());
            }
        });

//        holder.title.setText(data.getTitle());
//        holder.numIndicator.setText(String.format("%d/%d", realPosition + 1, size));
    }

    public HashMap<Integer, RecyclerView.ViewHolder> getVHMap() {
        return mVHMap;
    }

    @Override
    public void onBindView(RecyclerView.ViewHolder holder, PlayResourcesBean data, int position, int size) {
        // This method is not used as we override onBindViewHolder
    }

    static class VideoHolder extends RecyclerView.ViewHolder {
        final MyVideoPlayer video;
        final TextView title;
        final TextView numIndicator;

        VideoHolder(View itemView) {
            super(itemView);
            video = itemView.findViewById(R.id.banner_vp);
            title = itemView.findViewById(R.id.video_title);
            numIndicator = itemView.findViewById(R.id.video_numIndicator);
        }
    }

    static class ImageHolder extends RecyclerView.ViewHolder {
        final ImageView image;
        final TextView title;
        final TextView numIndicator;

        ImageHolder(View itemView) {
            super(itemView);
            image = itemView.findViewById(R.id.banner_image);
            title = itemView.findViewById(R.id.image_title);
            numIndicator = itemView.findViewById(R.id.image_numIndicator);
        }
    }
} 